import { newLineIntroductionApi, getBookingNoteApi } from '../../api/index'
import {Logger, setPage, replaceProtocol} from '../../utils/index'
import { TRACER_PAGES, PRODUCT_CODE } from '../../utils/constants'
const logger = new Logger()
const pageName = TRACER_PAGES['boatinfo']
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bookPolicy: '',
    boatDescList: [],
    currentTab: 'boatDesc',
    
    tabsList: [
      {
          tag: 'boatDesc',
          name: '游船介绍'
      },
      {
          tag: 'bookPolicy',
          name: '预订须知'
      }
    ],

    scrollTop: 0,
    scrollIntoView: 'fixedView'

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init(options)
  },

  onShow: function() {
    setPage(pageName, PRODUCT_CODE)
  },

  init: function(options) {
    
    const newLineParams = {
      lineId: options.lineId,
      shipId: options.shipId,
      shipName: options.shipName,
      supplierId: options.supplierId,
      voyageId: options.voyageId
    }
    this.getNewLineIntroduction(newLineParams)

    this.getBookingNote(options)
  },

  getNewLineIntroduction: function(params) {
    const that = this
    newLineIntroductionApi(params).then(response => {
      
      if(response.status !== 200) {
        logger.tips('网络错误')
        return
      }
      if(!response.data || response.data.code !== 10000) {
        logger.tips('请求失败或者没有数据')
        return
      }
      const description = response.data.data

      debugger
      if(description && description.length > 0) {
          const boatDescList = description.map((item, index) => {
            item.id = 'block_' + index
            if (item.descriptionInfo) {
                item.descriptionInfo = item.descriptionInfo.map((childItem, childIndex) => {
                    childItem.id = 'desc_' + childIndex
                    if (childItem.type === 1) {
                        childItem.description = replaceProtocol(childItem.description)
                    }
                    return childItem
                })
            }
            return item
          })

          this.setData({
            boatDescList
          })
      }
    })
  },

  getBookingNote: function({lineId}) {
    const that = this
    getBookingNoteApi({lineId}).then(response => {
      if(response.status !== 200) {
        logger.tips('网络错误')
        return
      }
      if(!response.data || response.data.Code !== 10000) {
        logger.tips('请求失败或者没有数据')
        return
      }
      that.setData({
        bookPolicy: response.data.Data
      })
    })
  },

  onTabHandler: function(e) {
    const tab = e.currentTarget.dataset.tab

    this.setData({
      currentTab: tab,
      scrollTop: 0
    })
  },
})